<?
require 'global.php';
if($customer_id==0) { header('Location:login.html'); exit; }

// 是国外的还是国内的
$overseas=OVERSEA;

$url='order_service';
$order_service=new DB(Torder_service,$conn);
$gee=$order_service;
$refund_reason = new DB(Trefund_reason, $conn);
$sales_order_detail=new DB(Tsales_order_detail,$conn);

// refer page 来源页面
$refer='none';
if(isset($_POST["refer"])) $refer=clean($_POST["refer"]);
if(isset($_GET["refer"])) $refer=clean($_GET["refer"]);
$source='none';	if(isset($_GET["source"])) $source=clean($_GET["source"]);

$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 操作: 查看/编辑/删除
$action=isset($_POST["action"])?clean($_POST["action"]):''; // 数据库操作: CURD
$id=0; if(isset($_GET["id"])) $id = clean($_GET["id"]);
$detail_id = 0; if(isset($_GET['detail_id'])) $detail_id = clean($_GET['detail_id']);

if($action=='insert'){ // sql动作: 添加
    $arr=array(
        'customer_id'=>$customer_id,
        'refund_status' => 1
    );
    if(isset($_POST['service_type'])) $arr['service_type']=$_POST['service_type'];
    if(isset($_POST['sales_order_id'])) $arr['sales_order_id']=$_POST['sales_order_id'];
    if(isset($_POST['sales_detail_id'])) $arr['sales_detail_id']=$_POST['sales_detail_id'];
    if(isset($_POST['p_id'])) $arr['product_id']=$_POST['p_id'];
    if(isset($_POST['v_id'])) $arr['variation_id']=$_POST['v_id'];
    if(isset($_POST['quantity'])) $arr['quantity']=$_POST['quantity'];
    if(isset($_POST['address'])) $arr['address']=$_POST['address'];
    if(isset($_POST['zip'])) $arr['zip']=$_POST['zip'];
    if(isset($_POST['contact_name'])) $arr['contact_name']=$_POST['contact_name'];
    if(isset($_POST['phone'])) $arr['phone']=$_POST['phone'];
    if(isset($_POST['contact_type'])) $arr['contact_type']=$_POST['contact_type'];
    if(isset($_POST['contact_other'])) $arr['contact_other']=$_POST['contact_other'];
    if(isset($_POST['refund_reason_id'])) $arr['refund_reason_id']=$_POST['refund_reason_id'];
    if(isset($_POST['refund_remark'])) $arr['refund_remark']=$_POST['refund_remark'];
    if(isset($_POST['description'])) $arr['description']=$_POST['description'];
    if($_FILES["pic"]["size"] > 0){
        $picdir=ROOT.'upload/';
        $pic=upfile($_FILES["pic"]);
        $image_resize=new Image_resize($picdir.$pic,600,600,0,$picdir.$pic);

        $arr['pic']=$pic;
    }
    $arr['create_time'] = date('Y-m-d H:i:s', time());
    if($gee->insert($arr)) {
        yell('success',$url.'.php');
    }
    else {}
}
if($action=='update'){ // sql动作: 更新
    $arr=array();
    $temid=$_POST['temid'];
    if(isset($_POST['sales_order_id'])) $arr['sales_order_id']=$_POST['sales_order_id'];
    if(isset($_POST['product_name'])) $arr['product_name']=$_POST['product_name'];
    if(isset($_POST['contact_name'])) $arr['contact_name']=$_POST['contact_name'];
    if(isset($_POST['quantity'])) $arr['quantity']=$_POST['quantity'];
    if(isset($_POST['phone'])) $arr['phone']=$_POST['phone'];
    if(isset($_POST['contact_type'])) $arr['contact_type']=$_POST['contact_type'];
    if(isset($_POST['contact_other'])) $arr['contact_other']=$_POST['contact_other'];
    if(isset($_POST['refund_reason_id'])) $arr['refund_reason_id']=$_POST['refund_reason_id'];
    if(isset($_POST['refund_remark'])) $arr['refund_remark']=$_POST['refund_remark'];
    if(isset($_POST['description'])) $arr['description']=$_POST['description'];
    //if(isset($_POST['pic'])) $arr['pic']=$_POST['pic'];
    if($_FILES["pic"]["size"] > 0){
        $picdir=ROOT.'upload/';
        $pic=upfile($_FILES["pic"]);
        $image_resize=new Image_resize($picdir.$pic,600,600,0,$picdir.$pic);

        $arr['pic']=$pic;
//        $tem=$gee->get_row($tem_ref_no,'ref_no');
//        $old_doc=ROOT.'upload/'.$tem['receipt_doc'];
//        unlink($old_doc);
    }
    try{
        $gee->update($arr,'id',$temid);
        yell('success',$url.'.php');
    }
    catch(Exception $e){
        yell('Failed, please retry.',$url.'.php');
    }
}

if($do=='view'){ // 功能: 查看列表
    $info=$gee->get_rows(1024,'customer_id',$customer_id);
}
if($do=='update'){ // 功能: 更新
    $info=$gee->get_row($id);
    $detail_info = product_detail($info['sales_detail_id']);
    $service_type = $code_array['service_type'];
}

// seo
$cate_name=WEB_ORDER_SALES_SERVICE;
$title=$cate_name.' - '.$config['SITE_TITLE_'.LANG];
$kwd=$title;
$dpn=$kwd;

$refund = $refund_reason->get_rows(1024, 'type', 1, 'seq_no');
$refund_arr = $code_array['service_status'];
$contact_type = $code_array['contact_type'];
//pre($refund);
//pre($refund_arr);
// view 加载视图


function product_detail($detail_id){
    global $product, $sales_order, $sales_order_detail, $product_variation, $option_label, $option_value, $lang, $province, $county, $city;
    $product_detail = array();
    $order_detail = $sales_order_detail->get_row($detail_id);
    $product_detail['p_id'] = $order_detail['product_id'];
    $product_detail['v_id'] = $order_detail['product_variation_id'];
    $product_info = $product->get_row($order_detail['product_id']);
    $product_detail['is_virtual'] = $product_info['is_virtual'];
    $str_var = '';
    $str_var .= $product_info['name_'.$lang].' ';
    if($order_detail['product_variation_id']>0){
        $tem_var=$product_variation->get_row($order_detail['product_variation_id']); // var
        if($tem_var['size']) $str_var.='['.$option_value[$tem_var['size']].'] ';
        if($tem_var['color']) $str_var.='['.$option_value[$tem_var['color']].'] ';
        if($tem_var['other1']) $str_var.='['.$option_value[$tem_var['other1']].'] ';
        if($tem_var['other2']) $str_var.='['.$option_value[$tem_var['other2']].'] ';
    }
    $product_detail['product_name'] = $str_var;
    $product_detail['quantity'] = $order_detail['quantity'];
    $product_detail['order_id'] = $order_detail['sales_order_id'];
    $sales_order_info = $sales_order->get_row($order_detail['sales_order_id']);
    $product_detail['sales_order_id'] = $sales_order_info['ref_no'];
    $tem_province=$province->get_name($sales_order_info['province'],$lang);
    $tem_addr=$tem_province;
    if(! empty($sales_order_info['city'])) { $tem_city=$city->get_name($sales_order_info['city'],$lang); $tem_addr.=$tem_city.'/'; }
    if(! empty($sales_order_info['county'])) { $tem_county=$county->get_name($sales_order_info['county'],$lang); $tem_addr.=$tem_county.'/'; }
    $tem_addr.=' '.$sales_order_info['address'];
    $product_detail['address'] = $tem_addr;
    $product_detail['phone'] = $sales_order_info['mobile'];
    $product_detail['contact_name'] = $sales_order_info['contact_name'];
    $product_detail['zip'] = $sales_order_info['zip'];

    return $product_detail;

}
require VIEW.$url.'.php';


// end of file
